package springboot.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import springboot.http.HttpClientTools;
import springboot.netty.NettySer;
import springboot.netty.SimpleServerHandler;
import springboot.tools.KernelTools;

import javax.annotation.PostConstruct;

@Service
public class AgvService {

    KernelTools km = new KernelTools();
    String v1OrderName=null;
    int A=0;
    public static final String V1="Vehicle-0001";
    @Scheduled(fixedDelay = 1500)
    public void changeStateTask() throws Exception {
        if (A==0){//不再更新orderName
            v1OrderName=km.getTranspotOrder(V1);
        }
        if(!v1OrderName.equals("N/A")) {
            if(!v1OrderName.startsWith("Park") && !v1OrderName.startsWith("Recharge")
                    && !v1OrderName.startsWith("TOrder") && !v1OrderName.startsWith("Move")) {
                //获得地堆放货地点属性
                String start=km.getPropertyByOrder(v1OrderName,"start");
                String end=km.getPropertyByOrder(v1OrderName,"end");
                /*不再跟新v1OrderName,直到任务结束或者失败*/
                if (A==0){
                    A=1;
                }
                String v1orderStatus = km.getOrderStatus(v1OrderName);
                if(v1orderStatus.equals("FINISHED")) {
                    /*任务完成,无任务存在,可以创建任务*/
                    if (start.equals("1024")&&(end.equals("1008")||end.equals("1009")||
                            end.equals("1010")||end.equals("1011"))){
                        SimpleServerHandler.TASK_A_TO_C=false;
                    }
                    if (start.equals("1024")&&(end.equals("1030")||end.equals("1031")||
                            end.equals("1032")||end.equals("1033") ||end.equals("1034")||
                            end.equals("1035")||end.equals("1036")||end.equals("1037")||
                            end.equals("1038")||end.equals("1039"))){
                        SimpleServerHandler.TASK_A_TO_D=false;
                    }
                    if (start.equals("1026")&&(end.equals("1008")||end.equals("1009")||
                            end.equals("1010")||end.equals("1011"))){
                        SimpleServerHandler.TASK_B_TO_C=false;
                    }
                    if (start.equals("1026")&&(end.equals("1030")||end.equals("1031")||
                            end.equals("1032")||end.equals("1033") ||end.equals("1034")||
                            end.equals("1035")||end.equals("1036")||end.equals("1037")||
                            end.equals("1038")||end.equals("1039"))){
                        SimpleServerHandler.TASK_B_TO_D=false;
                    }
                    HttpClientTools.sendGetData("http://localhost:9998/updateToYES/"+end,"utf-8");
                    A=0;
                }
                if(v1orderStatus.equals("FAILED")) {
                    /*任务失败可以创建任务*/
                    if (start.equals("1024")&&(end.equals("1008")||end.equals("1009")||
                            end.equals("1010")||end.equals("1011"))){
                        SimpleServerHandler.TASK_A_TO_C=false;
                    }
                    if (start.equals("1024")&&(end.equals("1030")||end.equals("1031")||
                            end.equals("1032")||end.equals("1033") ||end.equals("1034")||
                            end.equals("1035")||end.equals("1036")||end.equals("1037")||
                            end.equals("1038")||end.equals("1039"))){
                        SimpleServerHandler.TASK_A_TO_D=false;
                    }
                    if (start.equals("1026")&&(end.equals("1008")||end.equals("1009")||
                            end.equals("1010")||end.equals("1011"))){
                        SimpleServerHandler.TASK_B_TO_C=false;
                    }
                    if (start.equals("1026")&&(end.equals("1030")||end.equals("1031")||
                            end.equals("1032")||end.equals("1033") ||end.equals("1034")||
                            end.equals("1035")||end.equals("1036")||end.equals("1037")||
                            end.equals("1038")||end.equals("1039"))){
                        SimpleServerHandler.TASK_B_TO_D=false;
                    }
                    HttpClientTools.sendGetData("http://localhost:9998/updateToYES/"+end,"utf-8");
                    A=0;
                }
            }
        }
    }

    @PostConstruct
    public void firstStart() throws Exception {
        new NettySer().start(9945);
    }
}
